from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
import gevent
import t_close_main_windowrcc_rc

gevent_run_loop_exit = False
def gevent_run_loop():
    gevent.sleep(0.001)
    if not gevent_run_loop_exit:
        QtCore.QTimer.singleShot(5, gevent_run_loop)


class MyWidget(QtWidgets.QWidget):
    UNKNOW = 0
    MOVE = 1
    RESIZE = 2
    def __init__(self, parent=None):
        super().__init__(parent)
        self.mouse_position = QtCore.QPoint()
        self.show()
        self.current_mouse_track_status = self.UNKNOW
        self.prev_mouse_pos = None

    def _is_mouse_in_rect(self, rect, mouse_pos):
        top, bottom, left, right = rect

        print(rect)
        print(mouse_pos)

        x = mouse_pos.x()
        y = mouse_pos.y()

        if x >= left and x <= right:
            if y >= top and y <= bottom:
                return True
            else:
                return False
        else:
            return False

    def _build_move_active_rect(self):
        left = self.width()/6
        top = self.height()/6
        right = left + (self.width() * 2.0) / 3.0
        bottom = top + (self.height() * 2.0) / 3.0

        return (top, bottom, left, right)

    def paintEvent(self, event):
        qp = QtGui.QPainter(self)
        br = QtGui.QBrush(QtGui.QColor(100, 10, 10, 40))
        qp.setBrush(br)
        qp.drawRect(QtCore.QRect(self.rect()))
        # qp.drawRect(QtCore.QRect(self.begin, self.end))

    def _update_mouse_track_status(self, mouse_position):
        if self._is_mouse_in_rect(self._build_move_active_rect(), mouse_position):
            print("MOVE")
            self.prev_mouse_pos = mouse_position
            self.current_mouse_track_status = self.MOVE
        else:
            print("RESIZE")
            self.current_mouse_track_status = self.RESIZE

    def mousePressEvent(self, event):
        self.mouse_position = event.pos()
        self._update_mouse_track_status(self.mouse_position)
        self.update()

    def calc_new_size_by_mouse_position(self, mouse_pos):
        width = max(mouse_pos.x(), 10)
        height = max(mouse_pos.y(), 20)
        return width,height


    def _calc_view_pos(self, mouse_pos):
        pos = self.pos()
        return (pos.x() + mouse_pos.x() - self.width()/2, pos.y() + mouse_pos.y() - self.height()/2)

    def mouseMoveEvent(self, event):
        if self.current_mouse_track_status == self.UNKNOW:
            return

        self.mouse_position = event.pos()

        if self.current_mouse_track_status == self.RESIZE:
            new_view_size = self.calc_new_size_by_mouse_position(self.mouse_position)
            self.resize(new_view_size[0], new_view_size[1])
        else:
            new_view_pos = self._calc_view_pos(self.mouse_position)
            self.move(new_view_pos[0], new_view_pos[1])
        self.update()

    def mouseReleaseEvent(self, event):
        self.current_mouse_track_status = self.UNKNOW
        self.mouse_position = event.pos()
        self.update()


class WinForm(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(WinForm, self).__init__(parent)

        self.setWindowTitle("关闭主窗口")
        self.setWindowIcon(QtGui.QIcon(QtGui.QPixmap(":/pic/mywindow.ico")))
        self.resize(600, 200)
        self.close_main_window_button = QtWidgets.QPushButton("关闭窗口")
        self.close_main_window_button.setStyleSheet("QPushButton:hover { color: red }")

        QtWidgets.QToolTip.setFont(QtGui.QFont("SansSerif", 10))
        self.close_main_window_button.setToolTip("关闭窗口")

        self.close_main_window_button.clicked.connect(self.on_close_main_window_button_clicked)

        layout = QtWidgets.QHBoxLayout()

        self.drag_able_view = MyWidget(self)
        layout.addWidget(self.close_main_window_button)

        layout.addWidget(self.drag_able_view)

        main_frame = QtWidgets.QWidget()
        main_frame.setLayout(layout)

        self.setCentralWidget(main_frame)

        gevent.spawn(self.show_x_y)

    def on_close_main_window_button_clicked(self):
        global gevent_run_loop_exit
        sender = self.sender()
        print(sender.text(), " 被按下了")
        qApp = QtWidgets.QApplication.instance()
        gevent_run_loop_exit = True
        qApp.quit()

    def show_x_y(self):
        while True:
            print(self.x(), self.y())
            gevent.sleep(1)

if __name__ == '__main__':
    qApp = QtWidgets.QApplication(sys.argv)
    win = WinForm()
    win.show()
    QtCore.QTimer.singleShot(10, gevent_run_loop)
    sys.exit(qApp.exec_())